package edu.cuit.zhuyimeng.uaa.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import edu.cuit.zhuyimeng.framework.common.result.CommonResult;
import edu.cuit.zhuyimeng.uaa.controller.vo.LoginVO;
import edu.cuit.zhuyimeng.uaa.controller.vo.RegisterVO;
import edu.cuit.zhuyimeng.uaa.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "认证接口")
@Validated
@RequestMapping("/verify")
@RequiredArgsConstructor
public class VerificationController {

    private final IUserService userService;

    /**
     * 登录
     *
     * @return 登录结果
     */
    @Operation(summary = "登录",security = @SecurityRequirement(name = "Authorization"))
    @PostMapping("/login")
    public CommonResult<String> doLogin(@Valid @RequestBody LoginVO loginVO) {
        userService.login(loginVO.getUsername(), loginVO.getPassword(), loginVO.getIsRememberMe());
        return CommonResult.success();
    }

    /**
     * 注销
     *
     * @return 注销结果
     */
    @Operation(summary = "注销", security = @SecurityRequirement(name = "Authorization"))
    @PostMapping("/logout")
    @SaCheckLogin
    public CommonResult<Void> doLogout() {
        userService.logout();
        return CommonResult.success();
    }

    /**
     * 注册
     *
     * @return 注册结果
     */
    @Operation(summary = "注册",security = @SecurityRequirement(name = "Authorization"))
    @PostMapping("/register")
    public CommonResult<Void> doRegister(@Valid @RequestBody RegisterVO registerVO) {
        userService.register(registerVO.getUsername()
                , registerVO.getNickname()
                , registerVO.getPassword()
                , registerVO.getEmail()
                , registerVO.getPhone()
        );
        return CommonResult.success();
    }

}
